﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipper.Model;
using System.Threading;
using Global;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using Communication;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 反应条传送仓
    /// </summary>
    public class DeliveryWarehouse:ApplianceComponent
    {
        #region 变量
        //传送仓控制
        public static DeliveryControl DeliveryControl = new DeliveryControl();
        static System.Timers.Timer mTimerCountDown = new System.Timers.Timer();
        public static DateTime mdtStart;

        #endregion
        
        #region 属性
        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 状态
        /// </summary>
        public override ControlStatusEnum DetectionStatus
        {
            get
            {
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        public static ControlStatusEnum Status
        {
            get
            {
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        static AutoResetEvent mPause = new AutoResetEvent(false);//暂停
        static bool mIsPause;
        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }

        #endregion
        public DeliveryWarehouse()
        {
            mTimerCountDown.Elapsed += new System.Timers.ElapsedEventHandler((ss, ee) =>
                {
                    TimeSpan ts = mdtStart - DateTime.Now;
                    
                    if (ts < TimeSpan.Zero)
                    {
                        ServiceProvider.Monitor.DeliveryCountDown = "00:00";
                        mTimerCountDown.Stop();
                    }
                    else
                    {
                        ServiceProvider.Monitor.DeliveryCountDown = string.Format("{0:D2}:{1:D2}", ts.Minutes, ts.Seconds);
                    }
                });
            mTimerCountDown.Interval = 1000;
        }
        
        //记录步骤，应对故障。。。
        public override void Run(ReactionCup strip)
        {
            //WashingTank.WashTank1Status = ControlStatusEnum.BUSY;
            //Functions.LOG("logolog");
            ////detectionStatus = DestinationStatusEnum.Busy;//忙碌
            //mdtStart = DateTime.Now.AddSeconds(90);
            //mTimerCountDown.Start();
            ////do
            ////{
            ////try
            ////{
            //        strip.Note = "---deliverwarehouwe---" + Thread.CurrentThread.Name + "--" + strip.ID;
            //        Console.WriteLine(strip.Note);
            //        strip.DetectStartTime = DateTime.Now;
            //        if (ServiceProvider.IsSimulate)
            //        {
            //            Thread.Sleep(ServiceProvider.DeliverWareSleepTime);
            //            Transportation.TransportateNow(strip, this, NextComponent);//传送
            //        }
            //        else
            //        {
            //            DeliveryControl.CupIn();
            //            DeliveryControl.WaitStatus(1);
            //            if (AddReagent1AndSample.LastCup == null)
            //            {
            //                AddReagent1AndSample.Sample.Position();//样本定位  
            //                ServiceProvider.Monitor.RunningTestTubeShelf = ServiceProvider.WorkSheetVM.TestTubeShelfList[0];
            //            }
            //            Can.SendCan(Can.SA_MoveA_ID, "1AM100"+Can.strEnd);//滑轨A定位减速度
            //            Transportation.TransportA.LeftPosition();
            //            Thread.Sleep(500);
            //            DeliveryWarehouse.DeliveryControl.Back2000();
            //            DeliveryWarehouse.DeliveryControl.WaitStatus(2);
            //            Transportation.TransportA.WaitStatus(1);
            //            if (AddReagent1AndSample.LastCup == null)
            //            {
            //                AddReagent1AndSample.Sample.WaitStatus(1);//样本定位
            //            }
            //        }
            //        detectionStatus = ControlStatusEnum.IDLE;//空闲
            ////}
            ////catch (Exception ex)
            ////{
            ////    detectionStatus = ControlStatusEnum.MALFUNCTION;
            ////}
            ////}while (detectionStatus == ControlStatusEnum.MALFUNCTION);

            //NextComponent.Run(strip);
        }
        /// <summary>
        /// 暂停/恢复
        /// </summary>
        /// <param name="isPause">暂停true,恢复false</param>
        public static void Pause(bool isPause)
        {
            mIsPause = isPause;
            DeliveryControl.IsPause = isPause;
        }
        public override void Simulate(ReactionCup strip)
        {
            Thread.Sleep(ServiceProvider.DeliverWareSleepTime);
        }
        /// <summary>
        /// 工程师调试
        /// </summary>
        public  void RunEngineer(ReactionStrip strip)
        {
            DeliveryControl.CupIn();
            DeliveryControl.WaitStatus(1);
            Transportation.TransportA.LeftPosition();
            Transportation.TransportA.WaitStatus(1);

            DeliveryControl.Back2000();
            DeliveryControl.WaitStatus(2);
        }
        /// <summary>
        /// 复位
        /// </summary>
        /// <returns></returns>
        public static bool Reset()
        {
            DeliveryControl.Reset();
            DeliveryControl.WaitStatus(0);
            return true;
        }
    }
}
